package com.dap.configuration.service.app.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baidu.disconf.web.service.app.bo.App;
import com.baidu.disconf.web.service.app.dao.AppDao;
import com.baidu.disconf.web.service.app.form.AppNewForm;
import com.baidu.disconf.web.service.config.bo.Config;
import com.baidu.dsp.common.exception.FieldException;
import com.dap.configuration.service.app.service.AppExtensionMgr;
import com.dap.configuration.service.app.vo.AppListExtensionVo;
import com.dap.configuration.service.config.dao.ConfigExtensionDao;
/**
 * @author dengxiang
 * @version 2014-6-16
 */
@Service
public class AppMgrExtensionImpl implements AppExtensionMgr{
	
	@Autowired
    private AppDao appDao;
	
	@Autowired
	private ConfigExtensionDao configExtensionDao;
	
	@Override
	public List<AppListExtensionVo> getAppList(String name) {
		List<App> appList = new ArrayList<App>();
		List<AppListExtensionVo> list = new ArrayList<AppListExtensionVo>();
		if(StringUtils.isEmpty(name)){
			appList = appDao.getByIds(null);
		}else{
			App app = appDao.getByName(name);
			if(app != null){
				appList.add(app);
			}
		}
		for(App app:appList){
			AppListExtensionVo vo = new AppListExtensionVo();
			vo.setId(app.getId());
			vo.setName(app.getName());
			vo.setDescription(app.getDesc());
			vo.setCreateTime(app.getCreateTime());
			vo.setUpdateTime(app.getUpdateTime());
			list.add(vo);
		}
		return list;
	}
	
	@Override
	public AppListExtensionVo getByName(String name) {
		App app = appDao.getByName(name);
		AppListExtensionVo vo = new AppListExtensionVo();
		if(app==null){
			throw new FieldException("app", "查找应用信息不存在", null);
		}else{
			vo.setId(app.getId());
			vo.setName(app.getName());
			vo.setDescription(app.getDesc());
			vo.setCreateTime(app.getCreateTime());
			vo.setUpdateTime(app.getUpdateTime());
		}
		return vo;
	}

	@Override
	public AppListExtensionVo getById(long appId) {
		App app = appDao.get(appId);
		AppListExtensionVo vo = new AppListExtensionVo();
		if(app!=null){
			vo.setId(appId);
			vo.setName(app.getName());
			vo.setDescription(app.getDesc());
			vo.setEmails(app.getEmails());
			vo.setCreateTime(app.getCreateTime());
		}else{
			throw new FieldException("app", "查找应用信息不存在", null);
		}
		return vo;
	}

	@Override
	public void update(long id, AppNewForm appNewForm,String createTime) {
		App app = new App();
		app.setId(id);
		app.setName(appNewForm.getApp());
		app.setDesc(appNewForm.getDesc());
		app.setEmails(appNewForm.getEmails());
		app.setCreateTime(createTime);
		app.setUpdateTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		appDao.update(app);	
	}

	@Override
	public void validateDelete(long appId) {
		List<Config> list = configExtensionDao.getConfByAppEnv(appId, null);
		App app = appDao.get(appId);
		if(list.size() > 0){
			throw new FieldException("app", "删除应用与配置关联，无法删除", null);
		}
		if(app == null){
			throw new FieldException("app", "删除应用信息不存在", null);
		}
	}

	@Override
	public void delete(long appId) {
		appDao.delete(appId);
	}

	@Override
	public void validateUpdate(long id, String name) {
		App app1 = appDao.get(id);
		App app2 = appDao.getByName(name);
		if(StringUtils.isEmpty(name)){
			throw new FieldException("app", "更新应用名称不能为空", null);
		}
		if(StringUtils.isEmpty(app1)){
			throw new FieldException("app", "更新应用信息不存在", null);
		}else{
			if(!app1.getName().equals(name)){
				if(!StringUtils.isEmpty(app2)){
					throw new FieldException("app", "更新应用名称已存在", null);
				}
			}
		}
	}
	
}
